package com.sdk.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import com.sdk.util.phone.StorageHelper;

public class ImageManager {

	public static final String IMAGE_FILE_HEAD = "sex_image_";
	public static final String IMAGE_SAVED_PATH = "/sex_image/";
	private final String mFilePath;
	public String mUserSavedPath;
	private static ImageManager sManager;
	private final Context mContext;

	private ImageManager(Context context) {
		mContext = context;
		mFilePath = getFilePath();
		final File file = new File(mFilePath);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

	public String getFilePath() {
		String path;
		path = new StringBuilder(StorageHelper.getExternalStorageDirectory()
				.toString()).append(IMAGE_SAVED_PATH).toString();
		return path;
	}

	public static ImageManager getInstance(Context context) {
		if (null == sManager) {
			synchronized (ImageManager.class) {
				if (null == sManager) {
					sManager = new ImageManager(context);
				}
			}
		}
		return sManager;
	}

	/**
	 * Get Bitmap object from the server and try to save it into local file
	 * system.
	 * 
	 * @param urlAddress
	 * @return
	 */
	private Bitmap getBitmapFromServer(String urlAddress) {
		// Log.i(TAG, "get bitmap from server: " + urlAddress);
		Bitmap bitmap = null;
		InputStream is = null;
		try {
			final URL url = new URL(urlAddress);
			is = url.openStream();
			final byte[] buffer = new byte[4096];
			final ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
			int bytesRead = is.read(buffer);
			while (-1 != bytesRead) {
				baos.write(buffer, 0, bytesRead);
				bytesRead = is.read(buffer);
			}
			final byte[] data = baos.toByteArray();
			bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
			if (null != bitmap) {
			} else {
				Toast.makeText(mContext, "Time out", Toast.LENGTH_SHORT).show();
			}
		} catch (final Exception e) {
		} finally {
			if (null != is) {
				try {
					is.close();
				} catch (final IOException e) {
				}
			}
		}
		return bitmap;
	}

	/**
	 * Save the image to local file.
	 * 
	 * @param fileName
	 * @param data
	 */
	public void saveBitmap(String url) {
		final Date mDate = new Date();
		final long m = mDate.getTime();
		final String fileName = IMAGE_FILE_HEAD + m;
		try {
			saveMyBitmap(getBitmapFromServer(url), fileName);
		} catch (final IOException e) {
			e.printStackTrace();

		}

	}

	@SuppressWarnings("null")
	public void saveMyBitmap(Bitmap mBitmap, String bitName) throws IOException {
		final File f = new File(mFilePath + bitName + ".jpeg");
		f.createNewFile();
		FileOutputStream fOut = null;
		try {
			fOut = new FileOutputStream(f);
		} catch (final FileNotFoundException e) {
			e.printStackTrace();
		}
		mBitmap.compress(Bitmap.CompressFormat.PNG, 50, fOut);
		try {
			fOut.flush();
		} catch (final IOException e) {
			e.printStackTrace();
		}
		try {

			// mHandler.sendEmptyMessage(MSG_LOAD_FINISHED);
			fOut.close();
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}

}
